from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, func, Text
from .base import BaseDbModel, Base


class Meetings(BaseDbModel):
    __tablename__ = "meetings"
    __table_args__ = {
        "comment": "会议表",
    }
    id = Column(Integer, primary_key=True, autoincrement=True, comment='主键')
    title = Column(String(255), nullable=False, comment='会议标题')
    folder_path = Column(String(255), nullable=True, comment='会议存放路径')
    description = Column(Text, nullable=True, comment="会议描述")
    owner_id = Column(Integer, nullable=True, comment="会议创建者用户ID")
    start_time = Column(DateTime, nullable=True, comment="计划开始时间")
    end_time = Column(DateTime, nullable=True, comment="计划结束时间")

    def __repr__(self):
        return f"<Meeting id={self.id} title={self.title}>"


class MeetingsRecording(Base):
    __tablename__ = "meetings_recordings"
    __table_args__ = {
        "comment": "会议分段记录表",
    }
    id = Column(Integer, primary_key=True, index=True, autoincrement=True, comment="会议录音主键")
    meeting_id = Column(Integer, ForeignKey("meetings.id"), nullable=False, index=True, comment="所属会议ID")
    filename = Column(String(512), nullable=True, comment="原始上传文件名")
    url = Column(String(512), nullable=True, comment="文件存放路径")
    file_type = Column(String(128), nullable=True, comment="文件类型")
    ext = Column(String(20), nullable=True, comment="文件扩展")
    duration_ms = Column(Integer, nullable=True, comment="音频时长（毫秒）")
    uploaded_at = Column(DateTime, server_default=func.now(), comment="上传时间")
    processing_status = Column(String(50), nullable=False, default="uploaded",
                               comment="处理状态: uploaded/processing/done/failed")
    model_hint = Column(String(255), nullable=True, comment="建议用于转录的模型/配置（可由客户端传入）")
    metadata_json = Column(Text, nullable=True, comment="JSON 格式的额外元数据（例如设备/采样率等）")


class MeetingsTranscript(Base):
    __tablename__ = "meetings_transcripts"
    __table_args__ = {
        "comment": "会议转录表",
    }
    id = Column(Integer, primary_key=True, comment="转录片段主键", autoincrement=True)
    meeting_id = Column(Integer, ForeignKey("meetings.id"), nullable=False, index=True, comment="所属会议")
    recording_id = Column(Integer, ForeignKey("meetings_recordings.id"), nullable=True, index=True, comment="所属录音")
    start_ts_ms = Column(Integer, nullable=False, comment="片段开始时间（相对于录音，毫秒）")
    end_ts_ms = Column(Integer, nullable=False, comment="片段结束时间（毫秒）")
    speaker = Column(String(200), nullable=True, comment="说话人标识或标签")
    text = Column(Text, nullable=False, comment="转录文本")
    lang = Column(String(32), nullable=True, comment="识别语言代码")
    created_at = Column(DateTime, server_default=func.now(), comment="转录创建时间")


class MeetingsSummary(Base):
    __tablename__ = "meetings_summaries"
    __table_args__ = {
        "comment": "会议摘要表",
    }
    id = Column(Integer, primary_key=True, index=True)
    meeting_id = Column(Integer, ForeignKey("meetings.id"), nullable=False)
    content = Column(Text, nullable=False, comment="自动生成的摘要内容（可能是 json/markdown）")
    model_name = Column(String(255), nullable=True, comment="用于生成摘要的 LLM/chain 名称")
    created_at = Column(DateTime, server_default=func.now(), comment="摘要时间")
